package com.example.minademo.controller;

import com.example.minademo.entity.*;
import com.example.minademo.mina.MinaMessage;
import com.example.minademo.mina.MinaServiceManager;
import com.example.minademo.mina.UserSessionManager;
import com.example.minademo.service.impl.ControllerServiceImpl;
import com.example.minademo.service.impl.ScreenShotServiceImpl;
import com.example.minademo.service.impl.UserServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/shot")
public class ShotController {

    @Autowired
    private Gson mGson;

    @Autowired
    private UserServiceImpl mUserService;
    @Autowired
    private ControllerServiceImpl mControllerService;
    @Autowired
    private ScreenShotServiceImpl mShotService;


    //绑定用户
    @PostMapping(value = "/bindUser")
    public String bindControlledUser(@RequestParam("userId") Integer userId,
                                   @RequestParam("controlledUserName") String controlledUserName){
        User controller = mUserService.findUserById(userId);
        if(controller == null){
            return mGson.toJson(ResultData.error("用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        User controlled = mUserService.findUserByName(controlledUserName);
        if(controlled == null){
            return mGson.toJson(ResultData.error("要求绑定的用户不存在，请检查被绑定的用户名输入是否正确",
                    ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        Controller c = mControllerService.findByUserIdAndControlledUserId(userId,controlled.getId());
        if(c != null){
            return mGson.toJson(ResultData.error("用户已绑定", ErrorCode.Shot.CODE_USER_HAS_BINDED));
        }

        Controller conr = mControllerService.save(userId,controlled.getId(),controlled.getName());
        if(conr != null){
            return mGson.toJson(ResultData.success("绑定成功",null));
        }else{
            return mGson.toJson(ResultData.error("绑定失败",ErrorCode.Shot.CODE_BIND_USER_FAIL));
        }
    }

    //获取被控制列表
    @RequestMapping(value = "/getControlledUsers")
    public String getControlledUser(@RequestParam("userId") Integer userId){
        User controller = mUserService.findUserById(userId);
        if(controller == null){
            return mGson.toJson(ResultData.error("用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        List<User> controllers = mControllerService.findControlledUsersByUserId(userId);
        return mGson.toJson(ResultData.success("成功",controllers));
    }

    //请求截图
    @GetMapping(value = "/shot")
    public String requestScreenShot(@RequestParam("userId") Integer userId,
                                    @RequestParam("controlledUserId") Integer controlledUserId){
        User controller = mUserService.findUserById(userId);
        if(controller == null){
            return mGson.toJson(ResultData.error("用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        User controlled = mUserService.findUserById(controlledUserId);
        if(controlled == null){
            return mGson.toJson(ResultData.error("被控制的用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        UserSessionManager manager = UserSessionManager.getInstance();
        Long sessionId = manager.getSessionId(userId);
        if(sessionId > 0){
            //用户在线
            MinaServiceManager.getInstance().sendToClient(sessionId,
                    mGson.toJson(new MinaMessage(MinaMessage.MSG_SHOT,userId)));
            return mGson.toJson(ResultData.success("请求已发送",null));
        }else{
            return mGson.toJson(ResultData.error("用户不在线",ErrorCode.Shot.CODE_USER_OFFLINE));
        }

    }

    //上传截图
    @PostMapping(value = "/uploadShot")
    public String uploadShot(@RequestParam("userId") Integer userId,
                             @RequestParam("controllerId") Integer controllerId,
                             @RequestParam("imagePath") String imagePath){
        //被控制的
        User controlled = mUserService.findUserById(userId);
        if(controlled == null){
            return mGson.toJson(ResultData.error("用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        //请求截图的
        User controller = mUserService.findUserById(controllerId);
        if(controller == null){
            return mGson.toJson(ResultData.error("用户不存在", ErrorCode.User.CODE_USER_NOT_EXIST));
        }

        ScreenShot shot = mShotService.save(controllerId,userId,imagePath);
        if(shot != null){
            mControllerService.updateShotNumber(controllerId,userId);
            //通知请求者
            UserSessionManager manager = UserSessionManager.getInstance();
            Long sessionId = manager.getSessionId(userId);
            if(sessionId > 0){
                MinaServiceManager.getInstance().sendToClient(sessionId,
                        mGson.toJson(new MinaMessage(MinaMessage.MSG_SHOT_UPLOAD,userId)));
            }
            return mGson.toJson(ResultData.success("上传成功",null));
        }else{
            return mGson.toJson(ResultData.error("保存失败",ErrorCode.Shot.CODE_SAVE_SHOT_FAIL));
        }

    }



}
